001 /*
002 * Copyright 2005 Stephen McConnell
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.tools.tasks;
020
021 import net.dpml.library.Library;
022 import net.dpml.library.Resource;
023 import net.dpml.library.ResourceNotFoundException;
024
025 import net.dpml.tools.Context;
026
027 import org.apache.tools.ant.BuildException;
028
029 /**
030 * The plugin task handles the establishment of ant tasks, listeners, and antlibs derived
031 * from a classloader established by the tools sub-system.
032 *
033 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034 * @version 1.1.0
035 */
036 public class PluginTask extends net.dpml.transit.tools.PluginTask
037 {
038 private String m_ref;
039
040 /**
041 * Set the ref address of a plugin resource.
042 * @param ref a resource ref
043 */
044 public void setRef( String ref )
045 {
046 m_ref = ref;
047 }
048
049 /**
050 * Task initialization.
051 */
052 public void init()
053 {
054 super.init();
055 Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
056 }
057
058 /**
059 * Task execution.
060 */
061 public void execute()
062 {
063 if( null != m_ref )
064 {
065 Context context = (Context) getProject().getReference( "project.context" );
066 if( null == context )
067 {
068 final String error =
069 "Missing 'project.context' reference.";
070 throw new IllegalStateException( error );
071 }
072 Library library = context.getLibrary();
073 try
074 {
075 Resource resource = library.getResource( m_ref );
076 String uri = resource.getArtifact( "plugin" ).toURI().toString();
077 setUri( uri );
078 }
079 catch( ResourceNotFoundException e )
080 {
081 String message = e.getMessage();
082 throw new BuildException( message, e, getLocation() );
083 }
084 catch( Exception e )
085 {
086 final String error =
087 "Unexpected error while attempting to initiaze plugin task uri.";
088 throw new BuildException( error, e );
089 }
090 }
091
092 super.execute();
093 }
094 }